package com.gengzp.system.controller;

import com.gengzp.common.constants.PermissionConstants;
import com.gengzp.common.document.annotations.Api;
import com.gengzp.common.document.annotations.ApiOperation;
import com.gengzp.common.model.base.req.SearchByIdReq;
import com.gengzp.common.model.base.resp.Result;
import com.gengzp.common.model.pagination.resp.PaginationResult;
import com.gengzp.common.model.role.req.AddSysRoleReq;
import com.gengzp.common.model.role.req.EditSysRoleReq;
import com.gengzp.common.model.role.req.PageSysRoleReq;
import com.gengzp.common.model.role.resp.PageSysRoleResp;
import com.gengzp.common.model.role.resp.SearchSysRolePermissionsResp;
import com.gengzp.common.model.role.resp.SearchSysRoleResp;
import com.gengzp.permission.annotation.CheckPermission;
import com.gengzp.system.service.ISysRoleService;
import jakarta.validation.Valid;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

/**
 * @ClassName SysRoleController
 * @Description 系统角色管理模块控制层
 * @Author gengzp
 * @Date 2025/5/16 0:08
 */
@RestController
@RequestMapping("/role")
@Api(tags = "系统角色管理模块")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 添加系统角色
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加系统角色")
    @CheckPermission(PermissionConstants.ROLE_WRITE)
    public Result<Void> addSysRole(@Valid @RequestBody AddSysRoleReq req) {
        sysRoleService.addSysRole(req);
        return Result.getSuccessResult();
    }

    /**
     * 编辑系统角色
     */
    @PostMapping("/edit")
    @ApiOperation(value = "编辑系统角色")
    @CheckPermission(PermissionConstants.ROLE_WRITE)
    public Result<Void> editSysRole(@Valid @RequestBody EditSysRoleReq req) {
        sysRoleService.editSysRole(req);
        return Result.getSuccessResult();
    }

    /**
     * 根据id删除系统角色
     */
    @PostMapping("/remove_by_id")
    @ApiOperation(value = "根据id删除系统角色")
    @CheckPermission(PermissionConstants.ROLE_WRITE)
    public Result<Void> removeSysRoleById(@Valid @RequestBody SearchByIdReq req) {
        sysRoleService.removeSysRoleById(Long.valueOf(req.getId()));
        return Result.getSuccessResult();
    }

    /**
     * 分页查询系统角色
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询系统角色")
    @CheckPermission(PermissionConstants.ROLE_READ)
    public Result<PaginationResult<PageSysRoleResp>> pageQuerySysRole(@Valid @RequestBody PageSysRoleReq req) {
        // 处理not in字段的 String -> Long 数据类型转换
        if (CollectionUtils.isNotEmpty(req.getExcludeRoleIds())) {
            req.setExcludeRoleIdsLong(req.getExcludeRoleIds().stream().map(Long::valueOf).collect(Collectors.toList()));
            req.setExcludeRoleIds(null);
        }
        return Result.getSuccessResult(sysRoleService.pageQuerySysRole(req));
    }

    /**
     * 根据id查询系统角色详细信息
     */
    @PostMapping("/search_by_id")
    @ApiOperation(value = "根据id查询系统角色详细信息")
    @CheckPermission(PermissionConstants.ROLE_READ)
    public Result<SearchSysRoleResp> searchSysRoleById(@Valid @RequestBody SearchByIdReq req) {
        return Result.getSuccessResult(sysRoleService.searchSysRoleById(Long.valueOf(req.getId())));
    }

    /**
     * 根据id查询角色拥有的权限列表
     */
    @PostMapping("/search_permissions_by_id")
    @ApiOperation(value = "根据id查询角色拥有的权限列表")
    @CheckPermission(PermissionConstants.ROLE_READ)
    public Result<SearchSysRolePermissionsResp> searchSysRolePermissionsById(@Valid @RequestBody SearchByIdReq req) {
        return Result.getSuccessResult(sysRoleService.searchSysRolePermissionsById(Long.valueOf(req.getId())));
    }

}
